from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.system.entity.do.student_do import Student
from module_admin.system.entity.vo.student_vo import StudentModel, StudentPageQueryModel
from utils.page_util import PageUtil


class StudentDao:
    """
    学生模块数据库操作层
    """

    @classmethod
    async def get_student_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据ID获取学生详细信息

        :param db: orm对象
        :param id: ID
        :return: 学生信息对象
        """
        student_info = (
            (
                await db.execute(
                    select(Student)
                    .where(
                        Student.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return student_info

    @classmethod
    async def get_student_detail_by_info(cls, db: AsyncSession, student: StudentModel):
        """
        根据学生参数获取学生信息

        :param db: orm对象
        :param student: 学生参数对象
        :return: 学生信息对象
        """
        student_info = (
            (
                await db.execute(
                    select(Student).where(
                        Student.id == student.id if student.id else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return student_info

    @classmethod
    async def get_student_list(cls, db: AsyncSession, query_object: StudentPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取学生列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 学生列表信息对象
        """
        query = (
            select(Student)
            .where(
                Student.name.like(f'%{query_object.name}%') if query_object.name else True,
                Student.id == query_object.id if query_object.id else True,
            )
            .order_by(Student.id)
            .distinct()
        )
        student_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return student_list

    @classmethod
    async def add_student_dao(cls, db: AsyncSession, student: StudentModel):
        """
        新增学生数据库操作

        :param db: orm对象
        :param student: 学生对象
        :return:
        """
        db_student = Student(**student.model_dump(exclude={}))
        db.add(db_student)
        await db.flush()

        return db_student

    @classmethod
    async def edit_student_dao(cls, db: AsyncSession, student: dict):
        """
        编辑学生数据库操作

        :param db: orm对象
        :param student: 需要更新的学生字典
        :return:
        """
        await db.execute(update(Student), [student])

    @classmethod
    async def delete_student_dao(cls, db: AsyncSession, student: StudentModel):
        """
        删除学生数据库操作

        :param db: orm对象
        :param student: 学生对象
        :return:
        """
        await db.execute(delete(Student).where(Student.id.in_([student.id])))

